$(function(){
	//获取首页搜索跳转到该页面传过来的搜索值
	  var thisURL = document.URL;      
	  var  getval =thisURL.split('?')[1];    
	  var searchinfo= getval.split("=")[1]; 
	  //如果接受地址中传过来的中文，那么需要转码，不要会中文乱码例如%E8%BE%BE%E7%93%A6
	  searchinfo= decodeURIComponent(searchinfo)
	$(".searchInfo-input").val(searchinfo);
	$(".searchInfo-nums").css("display","block");
	$(".SearchInfo-tips").css("display","none");
	var searchContents=[
		{title:"标题1",content:"内容1",author:"Xxx",time:"2021/4/23"},
		{title:"标题2",content:"内容222222222222222222",author:"Xxx",time:"2021/4/23"},
		{title:"标题3",content:"内容3",author:"Xxx",time:"2021/4/23"},
		{title:"标题4",content:"内容4",author:"Xxx",time:"2021/4/23"},
		{title:"标题5",content:"内容5",author:"Xxx",time:"2021/4/23"},
		{title:"标题6",content:"内容6",author:"Xxx",time:"2021/4/23"},
		{title:"标题7",content:"内容7",author:"Xxx",time:"2021/4/23"},
	
		]
	$.ajax({
	          //请求方式
	          type : "get",
	          async:false,//同步
	          //请求地址
	          url : "http://localhost:3030/probablyexp",
	          
	          data :{searchinfo},
	          //请求成功
	          success : function(result) {
				searchContents=result.data;
				
				$(".searchInfo-nums").text("为你找到"+searchContents.length+"条数据")
				console.log(result)
				 
	          },
	          //请求失败，包含具体的错误信息
	          error : function(e){
	             alert("查找失败")
	          }
	      });
		  
		  
	
	// 搜索框js
	$(".searchInfo-input").focus(function(){
		$(this).css({
			"boxShadow": "0px 0px 0px 1px rgba(23,213,107,.5)",
		})
	})
	$(".searchInfo-input").blur(function(){
	  $(this).css("boxShadow","");
	});
	
	//
	$(".searchInfo-input").keyup(function(){
				  if(event.keyCode ==13){
				    $(".searchInfo-searchBtn").trigger("click");
				  }
				
		})
	
	//搜一搜按钮js
	$(".searchInfo-searchBtn").click(function(){
		
		 var  getsearchinfo =$(".searchInfo-input").val();   
		  if(getsearchinfo==""){
			  $(".SearchInfo-tips").css("display","block");
			$(".searchInfo-nums").css("display","none");
		  }else{
			  $(".searchInfo-nums").css("display","block");
			  $(".SearchInfo-tips").css("display","none");
			  document.location.href="搜索详情.html?searchinfo="+getsearchinfo;
			  console.log(getsearchinfo)
		  }
		    
	})
	
		// 分页js
		//搜索结果展示
		let authorname="";
		let releaseTime="";
		function addSearchContents(list,currIndex){	
			let href="";
			if(currIndex*5>list.length){
				for(let i=(currIndex-1)*5;i<list.length;i++){	
					href="经验详情.html?expId="+list[i].expId;
					$.ajax({
					          //请求方式
					          type : "get",
					          async:false,//同步
					          //请求地址
					          url : "http://localhost:3030/SearchUserInfo",
					          
					          data :{userAccount:list[i].userAccount},
					          //请求成功
					          success : function(result) {
								  console.log(result)
									authorname=result[0].username
									
					          },
					          //请求失败，包含具体的错误信息
					          error : function(e){
					             alert("查找失败")
					          }
					      });
						  releaseTime=list[i].content.date;
					var div1=$(`<div class="searchInfo-item">
						<div class="searchInfo-item-title"><a href=${href} class="searchInfo-item-title-a" >${list[i].title}</a></div>
						<div class="searchInfo-item-content">${list[i].content.details}</div>
						<div class="searchInfo-item-author"><span>${authorname}</span><span>${releaseTime}</span></div>
					</div>`)
					$(".searchInfo-contents").append(div1)
				
					
				}
			}
			else{
				for(let i=(currIndex-1)*5;i<=(currIndex*5)-1;i++){
							href="经验详情.html?expId="+list[i].expId;
							$.ajax({
							          //请求方式
							          type : "get",
							          async:false,//同步
							          //请求地址
							          url : "http://localhost:3030/SearchUserInfo",
							          
							          data :{userAccount:list[i].userAccount},
							          //请求成功
							          success : function(result) {
											authorname=result.username
											
							          },
							          //请求失败，包含具体的错误信息
							          error : function(e){
							             alert("查找失败")
							          }
							      });
							releaseTime=list[i].content.date;
						var div1=$(`<div class="searchInfo-item">
							<div class="searchInfo-item-title"><a href=${href} class="searchInfo-item-title-a" >${list[i].title}</a></div>
							<div class="searchInfo-item-content">${list[i].content.details}</div>
							<div class="searchInfo-item-author"><span>${authorname}</span><span>${releaseTime}</span></div>
						</div>`)
						$(".searchInfo-contents").append(div1)					
				}
			}
			
		}
		
	layui.use('laypage', function(){
	  var laypage = layui.laypage;
	  
	  //执行一个laypage实例
	  laypage.render({
	    elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
	    ,limit:5
		,count:searchContents.length //数据总数，从服务端得到
		,jump: function(obj){
			$(".searchInfo-contents").empty();
			console.log(obj)			
			addSearchContents(searchContents,obj.curr)
	
	  
    }
	,theme: '#1E9FFF',
	
	  });
	});
	
	
	
	
	
	
	
	// 结束括号
})